home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / Chia Windows X / Source / main.c
Encoding:
C/C++ Source or Header  |  2001-06-23  |  4.0 KB  |  193 lines

  1. #import <Carbon/Carbon.h>
  2. #import <pthread.h>
  3.  
  4. #define ABS(x) ( (x) < 0 ? -(x) : (x) )
  5.  
  6. // Functions to patch
  7. extern OSStatus
  8. CreateNewwindoW(WindowClass class, WindowAttributes attributes, const Rect *rect,
  9.     WindowRef *outWindow);
  10.  
  11. extern void 
  12. ShowwindoW(WindowRef window);
  13.  
  14. extern OSStatus
  15. CreateNewWindow(WindowClass class, WindowAttributes attributes, const Rect *rect,
  16.     WindowRef *outWindow)
  17. {
  18.     OSStatus    result;
  19.     
  20.     result = CreateNewwindoW(class, attributes, rect, outWindow);
  21.     
  22.     return result;
  23. }
  24.  
  25.  
  26.  
  27. Boolean KeyDown(KeyMap gkm, int k)
  28. {
  29.     return((((unsigned char *)gkm)[k>>3]>>(k&7))&1);
  30. }
  31.  
  32. #define kSizeBuffer    10
  33. #define kShift        0x39
  34. #define kXOff        .82
  35. #define    kYOff        .82
  36. #define kYAcc        .1
  37. #define kXAcc        .1
  38.  
  39. extern void 
  40. ShowWindow(WindowRef window)
  41. {
  42.     KeyMap    map;
  43.     
  44.     printf("ShowWindow called.\n");
  45.     
  46.     GetKeys(map);
  47.     if(window && !KeyDown(map, kShift))
  48.     {
  49.     Rect    endBounds;
  50.     Rect    bounds;
  51.     float    xOffset = kXOff, yOffset = kYOff;
  52.     float    xAcc = kXAcc, yAcc = kYAcc;
  53.     float    dx, dy;
  54.     float    x, y;
  55.     float    mult;
  56.     float    shortLength;
  57.     Point    center;
  58.     Str255    title;
  59.     short    fullTitleLength, i;
  60.     long    count;
  61.     Boolean    growing;
  62.     
  63.     GetWTitle(window, title);
  64.     fullTitleLength = title[0];
  65.     title[0] = 0;    // begin with the title undrawn - draw no characters
  66.     SetWTitle(window, title);
  67.     
  68.     GetWindowBounds(window, kWindowContentRgn, &endBounds);
  69.     center.h = (endBounds.right + endBounds.left) >> 1;
  70.     center.v = (endBounds.bottom + endBounds.top) >> 1;
  71.     dx = endBounds.right - center.h;
  72.     dy = endBounds.bottom - center.v;
  73.     
  74.     if(dx == 0 || dy == 0)    // We don't bother if the window is super-duper annorexic
  75.     {
  76.         ShowwindoW(window);
  77.         return;
  78.     }
  79.     
  80.     // Set the proportions of the beginning rectangle, velocity, and accellerations
  81.     if(dx < dy) {
  82.         mult = dy / dx;
  83.         yAcc *= mult;
  84.         yOffset *= mult;
  85.         x = kSizeBuffer;
  86.         y = kSizeBuffer * mult;
  87.         shortLength = dx;
  88.     } else {
  89.         mult = dx / dy;
  90.         xOffset *= mult;
  91.         xAcc *= mult;
  92.         x = kSizeBuffer * mult;
  93.         y = kSizeBuffer;
  94.         shortLength = dy;
  95.     }
  96.     
  97.     // Set our actual window rects
  98.     SetRect(&bounds, center.h - x, center.v - y, center.h + x, center.v + y);
  99.     
  100.     // If the window needs to shrink we set it to shrink
  101.     if(x > dx) {
  102.         xAcc = -xAcc;
  103.     }
  104.     if(y > dy) {
  105.         yAcc = -yAcc;
  106.     }
  107.     
  108.     // If our starting size matches the window size, we don't grow.
  109.     if(dx == x) {
  110.         xOffset = xAcc = 0;
  111.     }
  112.     if(dy == y) {
  113.         yOffset = yAcc = 0;
  114.     }
  115.     
  116.     // Set the new bounds of the window, and show
  117.     SetWindowBounds(window, kWindowContentRgn, &bounds);
  118.     ShowwindoW(window);
  119.     
  120.     growing = true; // begin growing
  121.     
  122.     // loop and grow
  123.     while(growing)
  124.     {
  125.         // Handle the x-coordinate
  126.         if(xOffset < 0) {
  127.         x += xOffset;
  128.         if(x <= dx)
  129.         {
  130.             xOffset = 0;
  131.             x = dx;
  132.         }
  133.         } else if(xOffset > 0) {
  134.         x += xOffset;
  135.         if(x >= dx)
  136.         {
  137.             xOffset = 0;
  138.             x = dx;
  139.         }
  140.         }
  141.         
  142.         // and the y-coord
  143.         if(yOffset < 0) {
  144.         y += yOffset;
  145.         if(y <= dy)
  146.         {
  147.             yOffset = 0;
  148.             y = dy;
  149.         }
  150.         } else if(yOffset > 0) {
  151.         y += yOffset;
  152.         if(y >= dy)
  153.         {
  154.             yOffset = 0;
  155.             y = dy;
  156.         }
  157.         }
  158.         
  159.         if( xOffset == 0 && yOffset == 0) { // if the offsets have both been set to 0, we're done
  160.         growing = false;
  161.         } else {
  162.         GetKeys(map);
  163.         if(KeyDown(map, kShift) == true)    // if they pressed shift, break the loop
  164.             growing = false;
  165.         else
  166.         {
  167.             // ... otherwise, we just set the bounds of the window.
  168.             bounds.left = (int)(center.h - x);
  169.             bounds.right = (int)(center.h + x);
  170.             bounds.top = (int)(center.v - y);
  171.             bounds.bottom = (int)(center.v + y);
  172.             SetWindowBounds(window, kWindowContentRgn, &bounds);
  173.             xOffset += xAcc;
  174.             yOffset += yAcc;
  175.         }
  176.         }
  177.     }
  178.     SetWindowBounds(window, kWindowContentRgn, &endBounds);
  179.     
  180. #define kTitleWait    5
  181.     for(i = 1; i <= fullTitleLength; i++)
  182.     {
  183.         count = TickCount() + kTitleWait;
  184.         title[0] = i;
  185.         SetWTitle(window, title);
  186.         while(count > TickCount())
  187.         ;    // animation waiting
  188.     }
  189.     }
  190.     
  191.     else    // Otherwise, we don't care so we tell the system to do its thing
  192.     ShowwindoW(window);
  193. }